//
//  HomeTabBarCtrl.m
//  cutter
//
//  Created by mac on 16/1/21.
//  Copyright © 2016年 e360. All rights reserved.
//
#import "SchemeCenterCtrl.h"
#import "IndexHomeCtrl.h"
#import "TMGlobal.h"
#import "MatchEdit.h"

@interface IndexHomeTableCell : UITableViewCell
@end
@implementation IndexHomeTableCell
-(void)setSelected:(BOOL)selected animated:(BOOL)animated{
    [super setSelected:selected animated:animated];
    //
    UIButton *view = [[self.contentView subviews] lastObject];
    if (view) {
        [view setSelected:selected];
    }
}
@end
//
@interface IndexHomeTable : UITableViewController
@end
@implementation IndexHomeTable
-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    if ([sender isKindOfClass:UITableViewCell.class]) {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
        if (indexPath) {
            id ctrl = [self.splitViewController.viewControllers lastObject];
            if ([ctrl isKindOfClass:UINavigationController.class]) {
                UINavigationController *root = ctrl;
                ctrl = [[root viewControllers] firstObject];
            }
            //
            if ([ctrl respondsToSelector:@selector(save:)]) {
                __weak IndexHomeTable *this = self;
                BOOL needSaved = [ctrl save:^(id target, id result) {
                    if (result) {
                        [TMGlobal setValue:[NSNumber numberWithInteger:indexPath.row] forKey:@"homeIndex"];
                    }else{
                        NSNumber *prevous = [TMGlobal valueForKey:@"homeIndex"];
                        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[prevous integerValue] inSection:0];
                        [this.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
                    }
                }];
                if (needSaved) {
                    return NO;
                }
            }
        }
    }
    return YES;
}
/*-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3) {
        return 0;
    }
    if (indexPath.row == 2 || indexPath.row == 7) {
        return 64;
    }
    return 54;
}*/
@end
//
@implementation IndexHomeCtrl
-(void)viewDidLoad{
    [super viewDidLoad];
    [self setMinimumPrimaryColumnWidth:254];
    [self setMaximumPrimaryColumnWidth:254];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(globalValueChange:) name:@"globalValueChange" object:nil];
    //
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHand:)];
    [self.view addGestureRecognizer:pan];
    [pan setCancelsTouchesInView:NO];
    [pan setDelegate:self];
    //
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHand:)];
    [self.view addGestureRecognizer:tap];
    [tap setCancelsTouchesInView:NO];
    [tap setDelegate:self];
    //
    [self setSelectedIndex:3];
    [self hidden:NO];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
//
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.maskView setFrame:self.view.bounds];
    [self viewSubviews];
}
-(void)viewSubviews{
    static CGFloat top = 50;
    //
    UIViewController *detail = [self.viewControllers lastObject];
    if (detail) {
        [detail.view setFrame:self.view.bounds];
    }
    //
    if (self.maskView) {
        [self.maskView setAlpha:1.0 + self.offset / self.minimumPrimaryColumnWidth];
        [self.view addSubview:self.maskView];
    }
    //
    UIViewController *master = [self.viewControllers firstObject];
    if (master) {
        CGRect frame = CGRectOffset(master.view.bounds, self.offset, 0);
        frame.size.height -= top;
        frame.origin.y += top;
        //
        [master.view setFrame:frame];
        [self.view addSubview:master.view];
    }
}
-(void)panHand:(UIPanGestureRecognizer*)sender{
    static CGPoint began;
    static CGFloat value;
    if(sender.state == UIGestureRecognizerStateBegan){
        began = [sender translationInView:self.view];
        value = [self offset];
    }else if(sender.state == UIGestureRecognizerStateChanged){
        CGPoint point = [sender translationInView:self.view];
        [self setOffset:MIN(0, MAX(-self.minimumPrimaryColumnWidth, value + point.x - began.x))];
        [self viewSubviews];
    }else{
        if (self.maskView.alpha > 0.5) {
            [self show:YES];
        }else{
            [self hidden:YES];
        }
    }
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if (touch.view == self.maskView) {
        return YES;
    }
    return NO;
}
-(void)tapHand:(UITapGestureRecognizer*)sender{
    [self hidden:YES];
}
-(void)show:(BOOL)animate{
    [self setOffset:0];
    if (animate) {
        [UIView beginAnimations:nil context:nil];
        [self viewSubviews];
        [UIView commitAnimations];
    }else{
        [self viewSubviews];
    }
}
-(void)hidden:(BOOL)animate{
    [self setOffset:-self.minimumPrimaryColumnWidth];
    if (animate) {
        [UIView beginAnimations:nil context:nil];
        [self viewSubviews];
        [UIView commitAnimations];
    }else{
        [self viewSubviews];
    }
}
-(void)showMenu{
    [self show:YES];
}
-(void)hiddenMenu{
    [self hidden:YES];
}
//
-(id)masterViewController{
    return [self.viewControllers firstObject];
}
-(id)detailViewController{
    if ([self.viewControllers count] > 0) {
        return [self.viewControllers lastObject];
    }
    return nil;
}
-(void)setSelectedIndex:(NSUInteger)selectedIndex{
    UINavigationController *root = [self masterViewController];
    if ([root isKindOfClass:UINavigationController.class]) {
        UITableViewController *master = [root.viewControllers firstObject];
        if (master) {
            [master setClearsSelectionOnViewWillAppear:NO];
            //
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
            [master.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
            //
            NSString *segue = [NSString stringWithFormat:@"segue%ld", (long)selectedIndex + 1];
            [master performSegueWithIdentifier:segue sender:nil];
        }
    }
}
//
-(void)globalValueChange:(NSNotification *)info{
    if ([info.object isEqualToString:@"homeIndex"]) {
        NSInteger index = [[TMGlobal valueForKey:info.object] integerValue];
        if (self.presentedViewController) {
            __weak typeof(self) this = self;
            [self dismissViewControllerAnimated:YES completion:^{
                [this setSelectedIndex:index];
            }];
        }else{
            [self setSelectedIndex:index];
        }
    }
}
@end